package com.hzdl.flower.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.flower.bean.Log;
import com.hzdl.flower.bean.Order;
import com.hzdl.flower.service.AorderService;
import com.hzdl.flower.util.HZDLJsonResult;
import com.hzdl.flower.util.HzdlJsonAjax;
import com.hzdl.flower.util.RentExcelUtil;
import com.hzdl.flower.vo.LogVo;
import com.hzdl.flower.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;

@Controller
@RequestMapping("aorder")
public class AorderController {
    @Autowired
    private AorderService aos;

    @RequestMapping("/qwe")
    private String aoderInit(){
        return "admin/aorder" ;
    }

    @ResponseBody
    @RequestMapping("findAorder")
    public HZDLJsonResult findAorder(OrderVo orderVo){
        PageInfo p = aos.findOrder(orderVo);
        return new HZDLJsonResult(0,"",p.getTotal(), p.getList());
    }

    @RequestMapping("checkExitChildren")
    @ResponseBody
    public HZDLJsonResult checkExitChildren(OrderVo orderVo){
        Order order =  aos.findById(orderVo.getId());
        return new HZDLJsonResult(0,"",order);
    }


    @RequestMapping("deleteOrder")
    @ResponseBody
    public HZDLJsonResult deleteOrder(Integer id){
        int row = aos.deleteById(id);
        if (row == 0){
            return HZDLJsonResult.DELETE_ERROR;
        }else {
            return HZDLJsonResult.DELETE_SUCCESS;
        }
    }

    @ResponseBody
    @RequestMapping("delAll")
    public HZDLJsonResult deAll(String ids){
        String[] arr = ids.split(",");
        Integer result=0;
        for (int a=0;a<arr.length;a++){
            result += aos.deleteById(Integer.parseInt(arr[a]));
        }
        if (result==0){
            return HZDLJsonResult.DELETE_ERROR;
        }else {
            return  HZDLJsonResult.DELETE_SUCCESS;
        }
    }

    @RequestMapping("exportOrder")
    @ResponseBody

    public HzdlJsonAjax exCarRent(OrderVo orderVo) {
        Order order= aos.findById(orderVo.getId());
        ByteArrayOutputStream excelByRent = RentExcelUtil.createExcelByRent(order, "czd-templet02.xls");

        if (excelByRent==null) {
            return new HzdlJsonAjax("导出失败", 0);
        }
        return new HzdlJsonAjax("导出成功", 1);
    }




}
